﻿<!DOCTYPE html>
<html lang="en">
<head>
    <title id='Description'>This example shows how to create a Grid from Observable Array data.</title>
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.edit.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.sort.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.columnsresize.js"></script> 
    <script type="text/javascript" src="../sampledata/generatedata.js"></script> 
    <script type="text/javascript" src="../../scripts/demos.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            var array = generatedata(2);

            var updateLog = function (observableArray) {
                var rows = "";
                for (var i = 0; i < observableArray.length; i++) {
                    rows += observableArray.toJSON(["firstname", "lastname", "productname", "quantity", "price", "total"], observableArray[i]);
                    rows += "<br/>";
                }
                $("#log").html(rows);
            }

            var observableArray = new $.jqx.observableArray(array, function (changed) {
                updateLog(this);
            });
            updateLog(observableArray);
            var source =
            {
                localdata: observableArray,
                datatype: "obserableArray",
                datafields:
                [
                    { name: 'firstname', type: 'string' },
                    { name: 'lastname', type: 'string' },
                    { name: 'productname', type: 'string' },
                    { name: 'quantity', type: 'number' },
                    { name: 'price', type: 'number' },
                    { name: 'total', type: 'number' }
                ]
            };

            var dataAdapter = new $.jqx.dataAdapter(source);
       
            $("#jqxgrid").jqxGrid(
            {
                width: 850,
                height: 150,
                source: dataAdapter,
                sortable: true,
                columnsresize: true,
                editable: true,
                selectionmode: "multiplecellsadvanced",
                columns: [
                  { text: 'Name', datafield: 'firstname', width: 120 },
                  { text: 'Last Name', datafield: 'lastname', width: 120 },
                  { text: 'Product', datafield: 'productname', width: 180 },
                  { text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
                  { text: 'Unit Price', datafield: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2' },
                  { text: 'Total', datafield: 'total', cellsalign: 'right', cellsformat: 'c2' }
                ]
            });

            $("#addItem").jqxButton();
            $("#deleteItem").jqxButton();
            $("#updateItem").jqxButton();
            $("#updatePath").jqxButton();
            $("#addItem").click(function () {
                var row = generatedata(1)[0];
                observableArray.push(row);
            });
            $("#deleteItem").click(function () {
                if (observableArray.length > 0) {
                    observableArray.splice(0, 1);
                }
            });
            $("#updatePath").click(function () {
                if (observableArray.length > 0) {
                    var row = generatedata(1)[0];
                    observableArray.set("0.firstname", row.firstname);
                    observableArray.set("0.lastname", row.lastname);
                }
            });
            $("#updateItem").click(function () {
                if (observableArray.length > 0) {
                    var row = generatedata(1)[0];
                    observableArray.set(0, row);
                }
            });
        });
    </script>
</head>
<body class='default'>
    <div id="jqxgrid"></div>
    <br /><br />
    <button id="addItem">Add Item</button>
    <button id="deleteItem">Delete Item</button>
    <button id="updateItem">Update Item</button>
    <button id="updatePath">Update Path</button>
    <br />
    <div id="log"></div>
</body>
</html>
